package ru.vsu.cs.pm.pages;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.UserManager;
import ru.vsu.cs.pm.models.User;

/**
 * User: Oleg
 * Date: 22.12.2010
 * Time: 21:46:33
 */
@MountPath(path = "login")
public class LoginPage extends WebPage {

    @SpringBean
    private UserManager userManager;

    public LoginPage() {
        add(new LoginForm("loginForm"));
        add(new RegisterForm("registerForm"));
    }

    private class LoginForm extends StatelessForm {

        private String username;
        private String password;

        public LoginForm(String wicketId) {
            super(wicketId);
            setModel(new CompoundPropertyModel(this));
            add(new TextField("username"));
            add(new PasswordTextField("password"));
            add(new FeedbackPanel("loginFeedback", new ComponentFeedbackMessageFilter(this)));
        }

        @Override
        protected void onSubmit() {
            if(login(username, password)) {
                if(!continueToOriginalDestination()) {
                    setResponsePage(getApplication().getHomePage());
                }
            } else {
                error("Invalid username or password");
            }
        }

        private boolean login(String username, String password) {
            if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
                User user = userManager.authenticate(username, password);
                if(user != null) {
                    PMSession.get().setAuthenticatedUser(user);
                    return true;
                }
            }
            return false;
        }
    }

    private class RegisterForm extends StatelessForm {

        private IModel<String> usernameModel = new Model<String>();
        private IModel<String> emailModel = new Model<String>();
        private IModel<String> passwordModel = new Model<String>();

        public RegisterForm(String wicketId) {
            super(wicketId);
            add(new TextField("usernameReg", usernameModel));
            add(new PasswordTextField("passwordReg", passwordModel));
            add(new FeedbackPanel("registerFeedback", new ComponentFeedbackMessageFilter(this)));
        }

        @Override
        protected void onSubmit() {
            String username = usernameModel.getObject();
            String password = passwordModel.getObject();

            if(StringUtils.isEmpty(username)) {
                error("Username is required.");
            }

            if(StringUtils.isEmpty(password)) {
                error("Password is required.");
            }

            User user = userManager.registerUser(username, password);

            if(user != null) {
                PMSession.get().setAuthenticatedUser(user);
                setResponsePage(getApplication().getHomePage());
            } else {
                error("User already exists.");
            }
        }
    }
}
